package com.dy.drp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.drp.dto.RolePowerDto;
import com.dy.drp.entity.Role;
import com.dy.drp.entity.Rolepower;
import com.dy.drp.mapper.RoleMapper;
import com.dy.drp.mapper.RolepowerMapper;
import com.dy.drp.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.drp.util.PageUtil;
import com.dy.drp.util.SelectUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dy
 * @since 2022-04-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolepowerMapper rolepowerMapper;

    @Override
    public PageUtil<Role> selectOnePageRole(PageUtil<Role> pageUtil) {
        List<Role> roles = roleMapper.selectOnePageRole(pageUtil);
        Integer integer = roleMapper.selectCount(null);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);
        pageUtil.setList(roles);
        return pageUtil;
    }

    @Override
    @Transactional
    public boolean insertRole(RolePowerDto rolePowerDto) {
        Integer integer = roleMapper.insertRole(rolePowerDto);
        if (integer <= 0){
            return false;
        }
        Integer integer1 = rolepowerMapper.insertRolePower(rolePowerDto);
        if (integer1 < 0){
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public boolean deleteRole(String roleName) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",roleName);
        Role role = roleMapper.selectOne(wrapper);

        QueryWrapper<Rolepower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",role.getRoleId());
        int delete = rolepowerMapper.delete(queryWrapper);
        if (delete <= 0){
            return false;
        }

        int delete1 = roleMapper.delete(wrapper);
        if (delete1 <= 0){
            return false;
        }
        return true;
    }

    @Override
    public RolePowerDto selectRolePowerByName(String roleName) {
        SelectUtil selectUtil = new SelectUtil();
        selectUtil.setName(roleName);
        return roleMapper.selectRolePowerByName(selectUtil);
    }

    @Override
    public boolean updateRolePower(RolePowerDto rolePowerDto) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",rolePowerDto.getRoleName());
        Role role = roleMapper.selectOne(wrapper);
        rolePowerDto.setRoleId(role.getRoleId());

        QueryWrapper<Rolepower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",role.getRoleId());
        int delete = rolepowerMapper.delete(queryWrapper);
        if (delete <= 0){
            return false;
        }
        Integer integer1 = rolepowerMapper.insertRolePower(rolePowerDto);
        if (integer1 < 0){
            return false;
        }
        return true;
    }
}
